package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenProductClassDO;
import com.ruoyi.project.men.domain.MenProductInfoDO;
import com.ruoyi.project.men.service.MenProductClassService;
import com.ruoyi.project.men.service.MenProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 核心产品Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menProductInfo")
public class MenProductInfoController extends BaseController
{
	@Autowired
	private MenProductInfoService menProductInfoService;
	@Autowired
	private MenProductClassService menProductInfoClassService;

	/**
	 * 查询核心产品列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenProductInfoDO menProductInfo)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menProductInfo);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MenProductInfoDO> menProductInfoList = menProductInfoService.list(stringObjectMap);
		if(menProductInfoList.size()>0){
			for (MenProductInfoDO menProductInfoDO : menProductInfoList) {
				MenProductClassDO menProductClassDO = menProductInfoClassService.get(menProductInfoDO.getClassId());
				if(menProductClassDO!=null){
					menProductInfoDO.setClassId(menProductClassDO.getName());
				}else{
					menProductInfoDO.setClassId("");
				}
			}
		}
		return getDataTable(menProductInfoList);
	}

	/**
	 * 导出核心产品列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:export')")
	@Log(title = "核心产品", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenProductInfoDO menProductInfo)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menProductInfo);
		List<MenProductInfoDO> menProductInfoList = menProductInfoService.list(stringObjectMap);
		if(menProductInfoList.size()>0){
			for (MenProductInfoDO menProductInfoDO : menProductInfoList) {
				MenProductClassDO menProductClassDO = menProductInfoClassService.get(menProductInfoDO.getClassId());
				if(menProductClassDO!=null){
					menProductInfoDO.setClassId(menProductClassDO.getName());
				}else{
					menProductInfoDO.setClassId("");
				}
			}
		}
		ExcelUtil<MenProductInfoDO> util = new ExcelUtil<MenProductInfoDO>(MenProductInfoDO.class);
		return util.exportExcel(menProductInfoList, "menProductInfo");
	}

	/**
	 * 获取核心产品详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		//获取分类
		AjaxResult ajax = AjaxResult.success();
		Map<String,Object> map = new HashMap<>();
		map.put("sort","ca_at");
		map.put("order","desc");
		map.put("delFlag","0");
		ajax.put("classList",menProductInfoClassService.list(map));
		ajax.put("data",menProductInfoService.get(id));
		return ajax;
	}

	/**
	 * 新增核心产品
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:add')")
	@Log(title = "核心产品", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenProductInfoDO menProductInfo)
	{
		menProductInfo.setId(IdUtil.simpleUUID());
		menProductInfo.setDelFlag(0);
		menProductInfo.setCaAt(new Date());
		return toAjax(menProductInfoService.save(menProductInfo));
	}

	/**
	 * 修改核心产品
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:edit')")
	@Log(title = "核心产品", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenProductInfoDO menProductInfo)
	{
		menProductInfo.setUpAt(new Date());
		return toAjax(menProductInfoService.update(menProductInfo));
	}

	/**
	 * 删除核心产品
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menProductInfo:remove')")
	@Log(title = "核心产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenProductInfoDO menProductInfo = menProductInfoService.get(string);
				menProductInfo.setDelFlag(1);
				menProductInfo.setUpAt(new Date());
				menProductInfoService.update(menProductInfo);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menProductInfoService.deleteMenProductInfoByIds(ids));
	}
}
